{% extends "layouts/base.html" %}

{% block title %} P4 Compiler {% endblock %}

<!-- Specific CSS goes HERE -->
{% block stylesheets %}

<link rel="stylesheet" href="/static/assets/css/codemirror/codemirror.css">
<link rel="stylesheet" href="/static/assets/css/codemirror/simplescrollbars.css">
<link rel="stylesheet" href="/static/assets/css/codemirror/dialog.css">
<link rel="stylesheet" href="/static/assets/css/codemirror/placeholder.css">

{% endblock stylesheets %}

{% block content %}

    <div class="pcoded-content">
        <div class="pcoded-inner-content">

            <div class="page-header">
                <div class="page-block">
                    <div class="row align-items-center">
                        <div class="col-md-12">
                            <div class="page-header-title">
                                <h5 class="m-b-10">P4 Compiler</h5>
                            </div>
                            <ul class="breadcrumb">
                                <li class="breadcrumb-item"><a href="/"><i class="feather icon-home"></i></a></li>
                                <li class="breadcrumb-item"><a href="javascript:">P4 Compiler</a></li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>

            <div class="main-body">
                <div class="page-wrapper">
                    <!-- [ Main Content ] start -->
                    <div class="row">
                        <div class="col-sm-12">
                            <div class="card">
                                <div class="card-header">
                                    <h5>Compile P4 Program</h5>
                                </div>
                                <div class="card-body">
                                    <h5>P4 Program</h5>
                                    <hr>
                                    <div class="row">
                                        <div class="col-md-12">
                                            <form>
                                                <div class="form-group">
                                                    <label for="compiler">Compiler / Target</label>
                                                    <select class="form-control" id="compiler">
                                                        <option>T4P4S</option>
                                                    </select>
                                                </div>

                                                <div class="form-group">
                                                    <label for="p4-program">P4 Program</label>
                                                    <select class="form-control" name="p4-program" id="p4-program">
                                                        <option value="l2switch">L2 Switch</option>
                                                        <option value="calc">Calculator</option>
                                                        <option value="reflector">Reflector</option>
                                                        <option value="firewall">Firewall</option>
                                                        <option value="stateful">Stateful Firewall</option>
                                                        <option value="basic_mirror">Basic Mirror</option>
                                                        <option value="arp_icmp">ARP/ICMP</option>
                                                        <option value="custom">Custom</option>
                                                    </select>
                                                </div>

                                                <div class="form-group d-none" id="p4-editor">
                                                    <label for="p4-code">P4 Code</label>
                                                    <span
                                                        data-toggle="tooltip"
                                                        data-html="true"
                                                        title="
                                                        <u>Keyboard Shortcuts</u>
                                                        <div style='text-align: left'>
                                                            <b>Ctrl-S</b>&nbsp&nbsp<em>Save</em> <br />
                                                            <b>Alt-G</b>&nbsp&nbsp<em>Jump to line</em> <br />
                                                            <b>Shift-Ctrl-F</b>&nbsp&nbsp<em>Replace</em> <br />
                                                            <b>Shift-Ctrl-R</b>&nbsp&nbsp<em>Replace all</em> <br />
                                                            <b>Ctrl-D</b>&nbsp&nbsp<em>Delete line</em> <br />
                                                            <b>Ctrl-Z</b>&nbsp&nbsp<em>Undo</em> <br />
                                                            <b>Ctrl-Y</b>&nbsp&nbsp<em>Redo</em> <br />
                                                            <b>Ctrl-F</b>&nbsp&nbsp<em>Start searching</em> <br />
                                                            <b>Ctrl-G</b>&nbsp&nbsp<em>Find next</em> <br />
                                                            <b>Shift-Ctrl-G</b>&nbsp&nbsp<em>Find previous</em> <br />
                                                            <b>Alt-F</b>&nbsp&nbsp<em>Search dialog doesn't autoclose, enter to find next, Shift-Enter to find previous</em> <br />
                                                        </div>
                                                        "
                                                    >
                                                        <i class="feather icon-info"></i>
                                                    </span>
                                                    <textarea class="form-control" name="p4-code" id="p4-code"></textarea>
                                                </div>

                                                <hr>

                                                <button type="button" class="btn btn-primary" onclick="upload()">
                                                    <i class="feather icon-upload"></i>Run
                                                </button>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- [ Main Content ] end -->
                </div>
            </div>
        </div>
    </div>

{% endblock content %}

<!-- Specific Page JS goes HERE  -->
{% block javascripts %}

<script src="/static/assets/js/codemirror/codemirror.js"></script>
<script src="/static/assets/js/codemirror/codemirror-p4-mode.js"></script>
<script src="/static/assets/js/codemirror/search.js"></script>
<script src="/static/assets/js/codemirror/searchcursor.js"></script>
<script src="/static/assets/js/codemirror/jump-to-line.js"></script>
<script src="/static/assets/js/codemirror/dialog.js"></script>
<script src="/static/assets/js/codemirror/closebrackets.js"></script>
<script src="/static/assets/js/codemirror/matchbrackets.js"></script>
<script src="/static/assets/js/codemirror/simplescrollbars.js"></script>
<script src="/static/assets/js/codemirror/placeholder.js"></script>


<script>
    csrftoken = getCookie('csrftoken');

    function upload() {
        const compiler = $('#compiler').val();
        const program = $('#p4-program').val();

        if (compiler == null || compiler == '') {
            return;
        }

        if (program == null || program == '') {
            return;
        }

        sendPostRequest("{% url 'upload_program' %}", {
            compiler: compiler,
            program: program,
            code : ''
        });
    }

    CodeMirror.commands.save = function(editor) {
        const compiler = $('#compiler').val();
        const program = $('#p4-program').val();

        if (compiler == null || compiler == '') {
            return;
        }

        if (program == null || program == '') {
            return;
        }

        sendPostRequest("{% url 'upload_program' %}", {
            compiler: compiler,
            program: program,
            code : btoa(editor.getValue())
        });

    };

    var cEditor = CodeMirror.fromTextArea(document.getElementById("p4-code"), {
        autoCloseBrackets: true,
        lineWiseCopyCut: true,
        scrollbarStyle: "overlay",
        placeholder: "Code goes here...",
        matchBrackets: true,
        matchBrackets: true,
        lineNumbers: false,
        smartIndent: true,
        indentUnit: 4,
        tabSize: 4,
        mode: "text/x-p4"
    });

    $('#p4-program').on('change', () => {
        const val = $('#p4-program').val();
        if (val === "custom") {
            $('#p4-editor').removeClass('d-none');
        } else {
            $('#p4-editor').addClass('d-none');
        }
    });
</script>

{% endblock javascripts %}
